package org.filesort.helpers;



import static com.google.common.base.Preconditions.*;

/**
 * fixme: Class Formats is for porn
 *
 * @author cheremin
 * @since 03.08.11,  13:33
 */
public final class Formats {
    public static final String[] SIZE_SUFFIXES = {
            "b",
            "Kb",
            "Mb",
            "Gb",
            "Tb",
            "Pb"
    };

    private Formats() {
        throw new AssertionError( "Not for instantiation" );
    }

    /**
     * @param size object size in bytes
     * @return human-readable string like 98Gb. Number will always be integer, and < 1536,
     *         except if it more then 1536Pb
     */
    public static String humanizedSize( final long size ) {
        checkArgument( size >= 0, "size(%s) must be >=0", size );
        String suffix = null;
        long _size = size;
        for ( int i = 0; i < SIZE_SUFFIXES.length; i++ ) {
            suffix = SIZE_SUFFIXES[i];
            if ( _size < 1024 * 3 / 2 ) {
                break;
            } else {
                _size >>= 10;
            }
        }
        return _size + " " + suffix;
    }
}
